
const utilsMixin = {
  methods: {
    // 转为时间戳 支持/Date(14444)/形式
    convertToTimestamp(dateObj) {
      if (!dateObj) {
        dateObj = new Date()
        return dateObj.getTime()
      } else if (typeof dateObj === 'string' && dateObj.indexOf('/Date(') === 0) {
        return parseInt(dateObj.replace('/Date(', '').replace(')/', ''), 10)
      } else if (typeof dateObj === 'string') {
        dateObj = new Date(dateObj.replace(/-/g, '/'))
        return dateObj.getTime()
      }
    },
    parseDate(val) {
      if (val && val !== null) {
        return new Date(val.replace(/-/g, '/'))
      } else {
        return undefined
      }
    },
    getFormatCurrency2: function(num) {
      if (num === undefined) {
        return num
      }
      num = num.toString().replace(/\$|\, /g, '')
      if (isNaN(num)) {
        num = '0'
      }
      num = parseFloat(num)
      const sign = !(num < 0)
      num = Math.abs(num)
      num = Math.floor(num * 100 + 0.50000000001)
      let cents = num % 100
      let unit = ''
      num = Math.floor(num / 100).toString()
      if (num.length > 8) {
        unit = '亿'
        cents = num.substring(num.length - 8)
        num = num.substring(0, num.length - 8)
      } else if (num.length > 4) {
        unit = '万'
        cents = num.substring(num.length - 4)
        num = num.substring(0, num.length - 4)
      }
      if (unit !== '') {
        if (cents.length > 2) {
          cents = cents.substring(0, 2)
        }
      }

      for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3))
      }
      return (((sign) ? '' : '-') + num + '.' + cents + unit)
    },
    getFormatCurrency: function(num) {
      if (num === undefined) {
        return num
      }
      num = num.toString().replace(/\$|\, /g, '')
      if (isNaN(num)) {
        num = '0'
      }
      num = parseFloat(num)
      const sign = !(num < 0)
      num = Math.abs(num)
      num = Math.floor(num * 100 + 0.50000000001)
      let cents = num % 100
      num = Math.floor(num / 100).toString()
      if (cents < 10) {
        cents = '0' + cents
      }
      for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3))
      }
      return (((sign) ? '' : '-') + num + '.' + cents)
    },
    dateFormat(format, dateObj) {
      if (!dateObj) {
        dateObj = new Date()
      }
      var date = {
        'M+': dateObj.getMonth() + 1,
        'd+': dateObj.getDate(),
        'H+': dateObj.getHours(),
        'm+': dateObj.getMinutes(),
        's+': dateObj.getSeconds(),
        'q+': Math.floor((dateObj.getMonth() + 3) / 3),
        'S+': dateObj.getMilliseconds()
      }
      if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in date) {
        if (new RegExp('(' + k + ')').test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
        }
      }
      return format
    },
    getFirstItem(line) {
      if (!line) {
        return ''
      }
      let i = 0
      for (; i < line.length; i++) {
        if (line[i] === ' ') {
          break
        }
      }
      return line.substr(0, i)
    },
    getEnumType(enumTypeName) {
      const enumType = enumTypes[enumTypeName]
      if (!enumType) {
        return []
      }
      return enumType
    }
  }
}

export { utilsMixin }
